package com.oocl.euc.common.utils;

import com.oocl.euc.core.requestform.model.RequestFormEmailMapping;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;

public class EmailMarkUtil {

    private EmailMarkUtil() {
        throw new IllegalStateException("Utility class");
    }

    private static Logger logger = LoggerFactory.getLogger(EmailMarkUtil.class);


    public static String processTemplate(String filePath, String templateName, String templateEncoding, RequestFormEmailMapping root) {
        try {
            Configuration config = new Configuration(Configuration.VERSION_2_3_22);
            config.setDirectoryForTemplateLoading(new File(filePath));

            Template template = config.getTemplate(templateName, templateEncoding);
            Writer out = new StringWriter();
            template.process(root, out);
            out.flush();
            out.close();

            return out.toString();

        } catch (IOException e) {
            logger.error("IOException",e);
        } catch (TemplateException e) {
            logger.error("TemplateException",e);
        }
        return null;
    }
}
